package com.villaday.idf.custom.pop;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.activity.H5Activity;

import razerdp.basepopup.BasePopupWindow;

public class AgreementConfigClosePop extends BasePopupWindow  {
    private Handler mHandler;
    private Context context;
    private Activity activity;
    private View.OnClickListener onClickListener;

    Gson gson = new GsonBuilder().create();

    public AgreementConfigClosePop(final Context context, final Activity activity, final View.OnClickListener onClickListener) {
        super(context);
        this.context = context;
        this.activity = activity;
        this.onClickListener = onClickListener;

        findViewById(R.id.tv_disagree).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                onClickListener.onClick(v);
            }
        });
        findViewById(R.id.tv_agree).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                onClickListener.onClick(v);

            }
        });
        TextView tv_content = findViewById(R.id.tv_content);

        mHandler = new Handler();
        setBackgroundColor(Color.parseColor("#B3000000"));


        buildAnima();
        setOutSideDismiss(false);
        setKeepSize(true);
        setBlurBackgroundEnable(true);

        final SpannableStringBuilder style = new SpannableStringBuilder();


        //设置文字
        style.append("       请放心，爱订房坚决保障您的隐私信息安全，您的信息仅用于为您提供服务或改善服务体验。\n      如果您确实无法认同此政策，可点击“不同意” 并退出应用。《爱订房服务协议》 《爱订房隐私政策》");

        //设置部分文字点击事件 《爱订房服务协议》
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {

                Intent intent = new Intent(activity, H5Activity.class);
                intent.putExtra("title", "");
                intent.putExtra("url", "https://res.villaday.com/static/pages/1907/19/190717-19.html");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                context.startActivity(intent);
            }
        };
        style.setSpan(clickableSpan, 83, 92, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置部分文字点击事件 《爱订房隐私政策》
        ClickableSpan clickableSpan1 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent(activity, H5Activity.class);
                intent.putExtra("title", "");
                intent.putExtra("url", "https://res.villaday.com/static/pages/1907/19/190717-19.html");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                context.startActivity(intent);
            }
        };
        style.setSpan(clickableSpan1, 93, 102, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_content.setText(style);

        //设置部分文字颜色
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#46AFFF"));
        ForegroundColorSpan foregroundColorSpan1 = new ForegroundColorSpan(Color.parseColor("#46AFFF"));

        style.setSpan(foregroundColorSpan, 83, 92, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(foregroundColorSpan1, 93, 102, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        //配置给TextView
        tv_content.setMovementMethod(LinkMovementMethod.getInstance());
        tv_content.setText(style);



    }



    // 必须实现，这里返回您的contentView
    // 为了让库更加准确的做出适配，强烈建议使用createPopupById()进行inflate
    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.pop_agreement_config_close);
    }


    private AnimationSet mAnimationSet;

    private void buildAnima() {
        ScaleAnimation mScaleAnimation = new ScaleAnimation(1f, 2f, 1f, 2f, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        mScaleAnimation.setDuration(200);
        mScaleAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        mScaleAnimation.setFillAfter(false);

        AlphaAnimation mAlphaAnimation = new AlphaAnimation(1, .2f);
        mAlphaAnimation.setDuration(400);
        mAlphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        mAlphaAnimation.setFillAfter(false);

        mAnimationSet = new AnimationSet(false);
        mAnimationSet.setDuration(400);
        mAnimationSet.addAnimation(mScaleAnimation);
        mAnimationSet.addAnimation(mAlphaAnimation);
        mAnimationSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dismiss();
                    }
                }, 150);

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }


    @Override
    protected Animation onCreateShowAnimation() {
        Animation showAnima = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,
                1f,
                Animation.RELATIVE_TO_PARENT,
                0,
                Animation.RELATIVE_TO_PARENT,
                0,
                Animation.RELATIVE_TO_PARENT,
                0);
        showAnima.setInterpolator(new DecelerateInterpolator());
        showAnima.setDuration(350);
        return showAnima;
    }

    @Override
    protected Animation onCreateDismissAnimation() {
        Animation exitAnima = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,
                0f,
                Animation.RELATIVE_TO_PARENT,
                1f,
                Animation.RELATIVE_TO_PARENT,
                0,
                Animation.RELATIVE_TO_PARENT,
                0);
        exitAnima.setInterpolator(new DecelerateInterpolator());
        exitAnima.setDuration(350);
        return exitAnima;
    }


}


